package com.work.dyduser.module.pagemine;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.work.dyduser.R;
import com.work.dyduser.base.BaseActivity;
import com.work.dyduser.bean.CommonBean;
import com.work.dyduser.bean.PhoneCheck;
import com.work.dyduser.httpretrofit.MyCallback;
import com.work.dyduser.httpretrofit.PostBody;
import com.work.dyduser.httpretrofit.RetrofitManager;
import com.work.dyduser.utils.CountdownButton;
import com.work.dyduser.utils.UIUtils;
import com.work.dyduser.utils.VerificationUtil;
import com.work.dyduser.utils.ViewUtils;
import com.work.dyduser.view.EyeEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;

/**
 * author: Aly
 * date :2018/12/7 09:30
 * version:1.0
 * description:注册
 */
public class RegisterActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_phonecode)
    EditText etPhonecode;
    @BindView(R.id.cbt_time)
    CountdownButton cbtTime;
    @BindView(R.id.et_password)
    EyeEditText etPassword;
    @BindView(R.id.et_welcomephone)
    EditText etWelcomephone;
    @BindView(R.id.cb_agreement)
    CheckBox cbAgreement;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.tv_gologin)
    TextView tvGologin;
    private int type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pagemine_register;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            mTitleBar.titleMiddle.setText("注册");
            tvRegister.setText("注册");
        } else {
            mTitleBar.titleMiddle.setText("修改密码");
            tvRegister.setText("修改密码");
        }
        ViewUtils.setOnClickListeners(this, cbtTime, tvAgreement, tvRegister, tvGologin);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.cbt_time:
                //获取短信验证码
                if (VerificationUtil.isPhone(etPhone.getText().toString())) {
                    getHttpCode(etPhone.getText().toString());
                    cbtTime.start();
                }
                break;
            case R.id.tv_agreement:
                //用户注册协议

                break;
            case R.id.tv_register:
                if (TextUtils.isEmpty(etPhone.getText().toString()) || TextUtils.isEmpty(etPhonecode.getText().toString()) || TextUtils.isEmpty(etPassword.getText().toString()) || TextUtils.isEmpty(etWelcomephone.getText().toString())) {
                    UIUtils.showToast("所填内容不能为空");
                    return;
                }
                if (type == 0) {
                    //注册
                    showLoaddialog();
                    getHttpchecksmscode();
                } else {
                    //修改密码
                    getHttpchecksmscode();
                }
                break;
            case R.id.tv_gologin:
                //登录
                finish();
                break;

        }
    }

    //校验验证码
    private void getHttpchecksmscode() {
        final String phone = etPhone.getText().toString();
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        map.put("smscode", etPhonecode.getText().toString());
        Call<PhoneCheck> call = RetrofitManager.getInstance().checksmscode(PostBody.toBody(map));
        call.enqueue(new MyCallback<PhoneCheck>() {
            @Override
            public void onSuccess(PhoneCheck response) {
                if (response.getErrcode() == 0) {
                    //验证码校验成功
                    if (type == 0) {
                        //注册
                        getHttpRegister(phone);
                    } else {
                        //修改密码
                        getHttpUpdatePassword(phone);

                    }
                } else {
                    UIUtils.showToast("验证码验证失败");
                    hideLoaddialog();
                }

            }


            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast("errMsg");
                hideLoaddialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    //更新密码
    private void getHttpUpdatePassword(String phone) {
        showLoaddialog();
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        map.put("user_secret", etPassword.getText().toString());
        Call<CommonBean> call = RetrofitManager.getInstance().updatepassword(PostBody.toBody(map));
        call.enqueue(new MyCallback<CommonBean>() {
            @Override
            public void onSuccess(CommonBean response) {
                if (response.isState()) {
                    UIUtils.showToast("密码修改成功");
                    finish();
                } else {
                    UIUtils.showToast("密码修改失败");
                }

            }

            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast(errMsg);
            }

            @Override
            public void onComplete() {
                hideLoaddialog();
            }
        });

    }

    //注册
    private void getHttpRegister(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        map.put("user_secrect", etPassword.getText().toString());
        Call<PhoneCheck> call = RetrofitManager.getInstance().register(PostBody.toBody(map));
        call.enqueue(new MyCallback<PhoneCheck>() {
            @Override
            public void onSuccess(PhoneCheck response) {
                if (response.getErrcode() == 0) {
                    UIUtils.showToast("注册成功,请登录");
                    finish();
                } else {
                    UIUtils.showToast(response.getMsg());
                }

            }

            @Override
            public void onFailure(int errorCode, String errMsg) {
            }

            @Override
            public void onComplete() {
                hideLoaddialog();
            }
        });

    }

    //获取验证码
    private void getHttpCode(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        Call<Object> call = RetrofitManager.getInstance().getsmscode(PostBody.toBody(map));
        call.enqueue(new MyCallback<Object>() {
            @Override
            public void onSuccess(Object response) {
                UIUtils.showToast("验证码发送成功");
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
